package org.itboys.product.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.common.collect.Maps;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.product.entity.ProductCategorySpec;
import org.springframework.stereotype.Service;

/**
 *  商品分类下的规格特征
 * @author weisky
 *
 */
@Service
public class ProductCategorySpecService extends BaseService<ProductCategorySpec, Long>{

	private static final long serialVersionUID = 4905379135919101992L;
	
	@Resource(name = "productDS")
	private MongoDataSource ds;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<ProductCategorySpec> getEntityClass() {
		return ProductCategorySpec.class;
	}
	
	/**
	 * 根据分类的ID，获取规格特征
	 * @param productCategoryId
	 * @return
	 */
	public List<ProductCategorySpec> findByProductCategoryId(Long productCategoryId){
		return super.findByField("productCategoryId", productCategoryId);
	}
	
	public boolean insert(ProductCategorySpec productCategorySpec){
		try{
			if(productCategorySpec.getId() != 0){
				update(productCategorySpec);
			}else{
				save(productCategorySpec);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("ProductCategorySpecService.insert error", e);
		}
		return false;
	}
	
	public boolean dele(Long id){
		try{
			delete(id);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("ProductCategorySpecService.dele error", e);
		}
		return false;
	}

	/**
	 * 检查分类下规格的数目
	 * @param categoryId
	 * @return
     */
	public Long checkSize(Long categoryId){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("productCategoryId", categoryId);
		param.put("isDeleted", 0);
		Long count = super.count(param);
		return count;
	}

}
